<%@ page import="java.util.Hashtable,
                 ro.gateway.aida.srv.EditorServlet,
                 ro.gateway.aida.usr.EditUserServlet,
                 ro.gateway.aida.srv.ICFService,
                 ro.gateway.aida.servlet.Constants,
                 ro.xblue.translator.LanguageBean,
                 ro.xblue.translator.TranslatorUtils,
                 ro.gateway.aida.fnd.*,
                 ro.gateway.aida.obj.db.CurrencyDB,
                 ro.gateway.aida.obj.*,
                 ro.gateway.aida.utils.HttpUtils,
                 ro.gateway.aida.db.PersistenceToken,
                 ro.gateway.aida.db.PersistenceManager,
			  ro.gateway.aida.admin.PropertyListItem,
			  java.util.ArrayList,
			  ro.gateway.aida.admin.PropertyListDB,
			  ro.gateway.aida.admin.PropertyList"%>
<%@ taglib uri="/WEB-INF/translator.tld" prefix="trans" %>
<% long zero_time = System.currentTimeMillis(); %>
<trans:initPage module="fundings" />
<html>
<%
     Hashtable bean = (Hashtable) request.getAttribute(EditorServlet.PNAME_BEAN);
     if (bean == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
     }

     String key = (String) bean.get(EditorServlet.PNAME_KEY);
     String action = (String) bean.get(EditorServlet.PNAME_ACTION);
     String ctx = request.getContextPath();
     // edit/delete/new
     String editing_action = (String) bean.get(EditorServlet.PNAME_ACTION);
     LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request, application);
     FundingItem item = (FundingItem) bean.get(EditorServlet.PNAME_BEAN);
     if (item == null)
     return;

     // yearly, bycat editors
     FundingYearlyItem[] y_items = item.getYearlyItemsArray();
     FundingByCatItem[] c_items = item.getByCatItemsArray();
     if (y_items == null) y_items = new FundingYearlyItem[0];
     if (c_items == null) c_items = new FundingByCatItem[0];
     Hashtable ybox = new Hashtable();
     ybox.put(FundingYearlyEditorServlet.PNAME_BOX_LIST, item.getYearlyItems());
     Hashtable cbox = new Hashtable();
     cbox.put(FundingByCatEditorServlet.PNAME_BOX_LIST, item.getByCatItems());
     cbox.put(FundingByCatEditorServlet.PNAME_BOX_ACTIVITY_ITEM,
             bean.get(FundingItemEditorServlet.PNAME_ACTIVITY_ITEM));
     session.setAttribute("yfndbox" + key, ybox);
     session.setAttribute("cfndbox" + key, cbox);
     // yearly, bycat editors

     Activity activity = ( Activity ) bean.get(FundingItemEditorServlet.PNAME_ACTIVITY_ITEM);
     // db stuff...
      PersistenceToken pers_token = PersistenceManager.
            tokenLookup(application);
     ICFService cfService = (ICFService) ((Hashtable) application.
            getAttribute( Constants.SERVICES )).get( Constants.SERVICE_CF );
     OrganizationInv[] orgs = activity.getOrgsByLRId(OrganizationRole.FINANCING_ORG_ROLE);
     if (orgs == null) orgs = new OrganizationInv[0];
     FundingType[] types = FundingTypePersMan.getManager(pers_token).getAll();
     if (types == null) types = new FundingType[0];
     PropertyList lstatuses = PropertyListDB.getManager(pers_token).getList("fndstatus");
	ArrayList statuses = null;
	if (lstatuses != null) {
		statuses = lstatuses.getItems();
	} else {
		statuses = new ArrayList();
	}
     Currency[] currencies = CurrencyDB.getManager(pers_token).getAll();
     if (currencies == null) currencies = new Currency[0];
     // db stuff...

     Object screen = request.getAttribute(EditUserServlet.PNAME_SCREEN);
     GenericActivityProperty[] categories = new GenericActivityProperty[0];
     if (activity != null) {
          categories = activity.getComponents();
          if (categories == null) categories = new GenericActivityProperty[0];
     }
%>
<head>
     <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
     <META HTTP-EQUIV="EXPIRES" CONTENT="0">
     <title><trans:showPageMessage label="title" /></title>
     <link rel="stylesheet" href="../alf.css" type="text/css">
     <script language="javascript">
     var wed_ysubeditor;
     function open_add_yitem() {
          var open_arg = "HEIGHT=220,WIDTH=500,SCROLLBARS=YES,LEFT=" +
               (window.screen.availWidth - 500) / 2 + ",TOP=" +
               (window.screen.availHeight - 220) / 2;
          wed_ysubeditor = window.open(
          "efndyearly?box=yfndbox<%=key%>",
          "ed_fndy_win", open_arg);
     };
     function open_add_yitem_ed(index) {
          var open_arg = "HEIGHT=220,WIDTH=500,SCROLLBARS=YES,LEFT=" +
               (window.screen.availWidth - 500) / 2 + ",TOP=" +
               (window.screen.availHeight - 220) / 2;
          wed_ysubeditor = window.open(
          "efndyearly?<%=EditorServlet.PNAME_ACTION%>=<%=EditorServlet.PV_ACTION_EDIT%>&box=yfndbox<%=key%>&index=" + index,
          "ed_fndy_win", open_arg);
     };
     function after_edit_ysubitem(caz) {
        wed_ysubeditor.close();
        if (caz == 0) {
            document.theform.<%=EditorServlet.PNAME_ACTION%>.value = '<%=EditorServlet.PV_ACTION_CONFIRM%>';
            document.theform.afteryedit.value="true";
            document.theform.submit();
        }
     };

     var wed_csubeditor;
     function open_add_citem() {
          var open_arg = "HEIGHT=400,WIDTH=550,SCROLLBARS=YES,LEFT=" +
               (window.screen.availWidth - 550) / 2 + ",TOP=" +
               (window.screen.availHeight - 400) / 2;
          wed_csubeditor = window.open(
          "efndbycat?box=cfndbox<%=key%>",
          "ed_fndc_win", open_arg);
     };
     function open_add_citem_ed(index) {
          var open_arg = "HEIGHT=400,WIDTH=550,SCROLLBARS=YES,LEFT=" +
               (window.screen.availWidth - 550) / 2 + ",TOP=" +
               (window.screen.availHeight - 400) / 2;
          wed_csubeditor = window.open(
          "efndbycat?<%=EditorServlet.PNAME_ACTION%>=<%=EditorServlet.PV_ACTION_EDIT%>&box=cfndbox<%=key%>&index=" + index,
          "ed_fndc_win", open_arg);
     };
     function after_edit_csubitem(caz) {
        wed_csubeditor.close();
        if (caz == 0) {
            document.theform.<%=EditorServlet.PNAME_ACTION%>.value = '<%=EditorServlet.PV_ACTION_CONFIRM%>';
            document.theform.aftercedit.value="true";
            document.theform.submit();
        }
     }
     </script>
</head>
<body>
<br><br>
<table align="center" width="90%" border="0" cellspacing="1" cellpadding="5" background=<%=ctx%>/images/pixelbgtable.gif>
    <form method="POST" action="<%=ctx%>/fnd/efnd" name="theform">
    <input type="hidden" name="<%=EditorServlet.PNAME_KEY%>" value="<%=key%>" />
    <input type="hidden" name="<%=EditorServlet.PNAME_ACTION%>" value="<%=EditorServlet.PV_ACTION_EDIT%>" />
    <input type="hidden" name="delyitems" value="" />
    <input type="hidden" name="delcitems" value="" />
    <input type="hidden" name="afteryedit" value="" />
    <input type="hidden" name="aftercedit" value="" />
<% if (EditUserServlet.PV_SCR_EDIT.equals(screen)) { %>
    <script language="javascript">
        function submit_theform(action) {
            if (document.theform.sbutton) {
                document.theform.sbutton.disabled=1;
            }
            if (document.theform.cbutton) {
                document.theform.cbutton.disabled=1;
            }
            if (document.theform.action) {
                document.theform.action.value=action;
            }

            document.theform.submit();
        }
    </script>
    <tr bgcolor="#FFFFFF">
      <td colspan="2">
      <trans:showPageMessage label="form.fundingtitle" />
      </td>
    </tr>
    <% if (HttpUtils.getValidTrimedString(request, "err", null) != null) { %>
    <tr bgcolor="#FFFFFF">
      <td colspan="2" class="text_search" bgcolor="#FFAAAA">
      <trans:showPageMessage label="form.incorrectdata" />
      </td>
    </tr>
    <% } %>
    <tr bgcolor="#FFFFFF">
      <td align="right" width="30%" class="text_search"><trans:showPageMessage label="form.org" /> :</td>
      <td width="70%" class="text_search">
        <select name="org_id">
        <option value="-1"><trans:showPageMessage label="form.selorg" /></option>
        <% for (int i = 0; i < orgs.length; i++) { %>
            <option <%=(orgs[i].getOrg_id() == item.getOrg_id()) ? "selected=\"true\" " : ""%>value="<%=orgs[i].getOrg_id()%>"><%=orgs[i].getOrg().getName()%></option>
        <% } %>
        </select>
      </td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td align="right" width="30%" class="text_search"><trans:showPageMessage label="form.type" /> :</td>
      <td width="70%" class="text_search">
        <select name="type_id">
        <option value="-1"><trans:showPageMessage label="form.seltype" /></option>
        <% for (int i = 0; i < types.length; i++) { %>
            <option <%=(types[i].getId() == item.getType_id()) ? "selected=\"true\" " : ""%>value="<%=types[i].getId()%>"><%=types[i].getName(lang_bean.language)%></option>
        <% } %>
        </select>
      </td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td align="right" width="30%" class="text_search"><trans:showPageMessage label="form.status" /> :</td>
      <td width="70%" class="text_search">
        <select name="status_id">
        <option value="-1"><trans:showPageMessage label="form.selstatus" /></option>
	   <% for (int i = 0; i < statuses.size(); i++) { %>
		<% PropertyListItem it =  (PropertyListItem) statuses.get(i);%>
            <option <%=(it.getId() == item.getStatus_id()) ? "selected=\"true\" " : ""%>value="<%=it.getId()%>"><%=it.getString(lang_bean.language)%></option>
        <% } %>
        </select>
      </td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td align="right" width="30%" class="text_search"><trans:showPageMessage label="form.amount" /> :</td>
      <td width="70%" class="text_search">
        <input type="text" size="20" name="amount" value="<%=item.getAmount()%>" /><br>
        <select name="currency">
        <option value=""><trans:showPageMessage label="form.selcurrency" /></option>
        <% for (int i = 0; i < currencies.length; i++) { %>
            <option <%=(currencies[i].getIso3().equals(item.getCurrency())) ? "selected=\"true\" " : ""%>value="<%=currencies[i].getIso3()%>"><%=currencies[i].getIso3()%> - <%=currencies[i].getName()%></option>
        <% } %>
        </select>
      </td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td align="right" width="30%" class="text_search"><trans:showPageMessage label="form.byyearlist" /> :</td>
      <td width="70%" class="text_search" align="center">
      <!-- YEARLY LIST -->
        <table width="90%" cellpadding="5" border="1" cellspacing="0">
        <% if (y_items.length == 0) { %>
            <tr>
                <td class="text_search" coslpan="3">
                    &middot; <trans:showPageMessage label="form.noitem" />
                </td>
            </tr>
        <% } else { %>
            <% for (int i = 0; i < y_items.length; i++) { %>
            <tr>
                <td width="5%">
                    <input type="checkbox" name="y_item_idx" value="<%=i%>">
                </td>
                <td width="90%">
                    <%=FundingToHtml.fundingYearlyItemToHTML(y_items[i],lang_bean.language,
                            "text_search","text_search")%>
                </td>
                <td width="5%">
                    <a class="text_searchBold" href="javascript:open_add_yitem_ed(<%=i%>);">[edit]</a
                </td>
            </tr>
            <% } %>
        <% } %>
        <tr>
            <td class="text_search" colspan="3">
                <% if (y_items.length > 0) { %>
                <input type="button" value="Delete selected"
                onclick="javascript:document.theform.action.value='<%=EditorServlet.PV_ACTION_CONFIRM%>';document.theform.delyitems.value='true';document.theform.submit();" />
                <% } %>
                <input type="button" value="Add item"
                onclick="javascript:open_add_yitem();" />
            </td>
        </tr>
      </table>
      <!-- YEARLY LIST -->
      </td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td align="right" width="30%" class="text_search"><trans:showPageMessage label="form.bycatlist" /> :</td>
      <td align="center" width="70%" class="text_search">
      <!-- BY CAT LIST -->
        <table width="90%" cellpadding="5" border="1" cellspacing="0">
        <% if (c_items.length == 0) { %>
            <tr>
                <td class="text_search" coslpan="3">
                    &middot; <trans:showPageMessage label="form.noitem" />
                </td>
            </tr>
        <% } else { %>
            <% for (int i = 0; i < c_items.length; i++) { %>
            <tr>
                <td width="5%">
                    <input type="checkbox" name="c_item_idx" value="<%=i%>">
                </td>
                <td width="90%">
                    <%=FundingToHtml.fundingByCatItemToHTML(c_items[i],lang_bean.language,
                            "text_search","text_search", application, request)%>
                </td>
                <td width="5%">
                    <a class="text_searchBold" href="javascript:open_add_citem_ed(<%=i%>);">[edit]</a
                </td>
            </tr>
            <% } %>
        <% } %>
        <tr>
            <td class="text_search" colspan="3">
                <% if (c_items.length > 0) { %>
                <input type="button" value="Delete selected"
                onclick="javascript:document.theform.action.value='<%=EditorServlet.PV_ACTION_CONFIRM%>';document.theform.delcitems.value='true';document.theform.submit();" />
                <% } %>
                <% if (categories.length < 1) { %>
                    .. add some components first ..
                <% } else { %>
                     <input type="button" value="Add item"
                     onclick="javascript:open_add_citem();" />
                <% } %>
            </td>
        </tr>
      </table>
      <!-- BY CAT LIST -->
      </td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td colspan="2">
        <input name="sbutton" type="button" class="butonsave" value="<trans:showPageMessage label="form.bsave" />"
        onclick="javascript:submit_theform('<%=EditorServlet.PV_ACTION_CONFIRM%>');">
        <input name="cbutton" type="button" class="butonsave" value="<trans:showPageMessage label="form.bcancel" />"
        onclick="javascript:submit_theform('<%=EditorServlet.PV_ACTION_CANCEL%>');">
      </td>
    </tr>
<% } else if (FundingItemEditorServlet.PV_SCREEN_CANCEL.equals(screen)) { %>
    <script language="javascript">
        opener.after_add_fitem(1);
    </script>
<% } else if (FundingItemEditorServlet.PV_SCREEN_SAVED.equals(screen)) { %>
    <script language="javascript">
        opener.after_add_fitem(0);
    </script>
<% } else { %>
no screen defined: <%=screen%>
<% } %>
    </form>
</table>
<br><br>
</body>
</html>
<!-- <%=System.currentTimeMillis() - zero_time%> -->
